import '@/api/car/types'
import { fetchList } from '@/api/car'
import {
  UPDATE_CARINFO
} from './action-types'

// eslint-disable-next-line import/prefer-default-export
export const carInfo = ({ page = 1, list = [] }) => {
  return async dispatch => {
    // 获取车辆列表信息
    const result = await fetchList({
      perPage: 50,
      page,
      sort: '-isDefault,-isCertified,-id'
    })
    const data = page > 1 ? list.concat(result.data.data) : result.data.data

    // console.log(data)
    const currentCar = data.find(v => {
      if (v.isDefault) {
        return v
      }
    })
    // console.log(currentCar)
    const resInfo = {
      list: data,
      currentCar: currentCar
    }
    // console.log(resInfo)
    // Taro.setStorageSync(IS_CARINFO, resInfo)
    dispatch({
      type: UPDATE_CARINFO,
      carInfo: resInfo
    })
    return new Promise(resolve => {
      resolve(resInfo)
    })
  }
}
